package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.erp.dto.ProcessEmployeeDto;
import com.yc.sole.erp.entity.ProcessEmployee;
import com.yc.sole.erp.mapper.ProcessEmployeeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author yizuomin
 * @date 2024/3/7 18:45
 **/
@Service
public class ProcessEmployeeRepository extends ServiceImpl<ProcessEmployeeMapper, ProcessEmployee> {
    /**
     * 根据工序id查询员工id
     *
     * @param processId
     * @return
     */
    public Set<Long> selectEmployeeIdByProcessId(Long processId) {
        LambdaQueryWrapper<ProcessEmployee> wrapper = Wrappers.<ProcessEmployee>lambdaQuery().eq(ProcessEmployee::getProcessId, processId);
        return this.list(wrapper).stream().map(ProcessEmployee::getEmployeeId).collect(Collectors.toSet());
    }

    /**
     * 根据工序id删除
     *
     * @param processId
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByProcessId(Long processId) {
        LambdaQueryWrapper<ProcessEmployee> wrapper = Wrappers.<ProcessEmployee>lambdaQuery().eq(ProcessEmployee::getProcessId, processId);
        this.remove(wrapper);
    }

    /**
     * 新增
     *
     * @param processEmployeeDtoList
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveProcessEmployee(Collection<ProcessEmployeeDto> processEmployeeDtoList) {
        List<ProcessEmployee> processEmployees = BeanUtil.copyToList(processEmployeeDtoList, ProcessEmployee.class);
        this.saveBatch(processEmployees);
    }
}
